package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sneakerappforme1105.GoodsDetailActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.Address;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;

import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter{
    private List<Address> mList;
    private Context mContext;
    private updateAddressListener mUpdateListener; // 点击修改图标，进行修改地址
    private OnButtonListener mButtonListener;
    private OnClickListener mClickListener;

    public interface updateAddressListener{
        // 修改地址接口
        public void updateAddress(View v, int position);    // 点击修改地址
    }

    public interface OnClickListener{
        // 修改地址接口
        public void click(View v, int position);    // 点击修改地址
    }

    // 定义一个循环视图列表项的长按监听器接口
    public interface OnButtonListener {
        void onButtonClick(View view, int position);
    }

    public void setUpdateListener(updateAddressListener mListener){
        this.mUpdateListener = mListener;
    }

    public void setButtonListener(OnButtonListener mListener){
        this.mButtonListener = mListener;
    }

    public void setClickListener(OnClickListener mListener){
        this.mClickListener = mListener;
    }

    public AddressAdapter(List<Address>mList, Context mContext){
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        // 根据单条数据的xml得到视图
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_address,viewGroup,false);
        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        // 在这里设置监听
        Address item = mList.get(position);
        ItemHolder holder = (ItemHolder) viewHolder;
        holder.bindBean(item);
        holder.iv_xg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mUpdateListener!=null){
                    mUpdateListener.updateAddress(view, holder.getAdapterPosition());
                }
            }
        });  // 弹出窗口
        holder.bt_delAddress.setOnClickListener(view -> {
            if(mButtonListener!=null){
                mButtonListener.onButtonClick(view,holder.getAdapterPosition());
            }
        });// 点击删除

        holder.ll_address.setOnClickListener(view -> {
            if(mClickListener!=null){
                mClickListener.click(view,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    private class ItemHolder extends RecyclerView.ViewHolder {
        private TextView tv_address;
        private ImageView iv_xg;
        private TextView tv_username;
        private TextView tv_tel;
        private Button bt_delAddress;
        private LinearLayout ll_address;

        public ItemHolder(View itemView) {
            super(itemView);
            tv_address = itemView.findViewById(R.id.tv_address);
            iv_xg = itemView.findViewById(R.id.iv_xg);
            tv_username = itemView.findViewById(R.id.tv_username);
            tv_tel = itemView.findViewById(R.id.tv_tel);
            bt_delAddress = itemView.findViewById(R.id.bt_delAddress);
            ll_address = itemView.findViewById(R.id.ll_address);
        }

        void bindBean(Address item){

            tv_address.setText(item.getDetailedAddress());
            tv_username.setText(item.getUsername());
            tv_tel.setText(item.getPhoneNumber());
        }
    }
}
